/**
 * 
 */
package idv.takeshi.software.productline.bookstore.presentation.web.filter;

import idv.takeshi.software.productline.bookstore.application.service.BrowseBooksService;
import idv.takeshi.software.productline.bookstore.application.service.Constants;
import idv.takeshi.software.productline.bookstore.domain.model.category.Category;
import idv.takeshi.software.productline.bookstore.domain.model.shoppingcart.ShoppingCart;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Initial global session attributes
 * @author takeshi
 *
 */
public class InitialSessionAttributesFilter implements Filter {
	
	private BrowseBooksService browseBooksService;
	
	private Long defaultSidebarCategoryId;
	
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws ServletException, IOException {
		
		Category rootCategory = null;
		Category sidebarCategory = null;
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpSession session = httpRequest.getSession();
		ShoppingCart shoppingCart = null;
		
		if(null == session.getAttribute(Constants.ROOT_CATEGORY_NAME)){
			rootCategory = browseBooksService.getRootCategory();
		} else {
			rootCategory = (Category) session.getAttribute(Constants.ROOT_CATEGORY_NAME);
		}
		
		if(null == session.getAttribute(Constants.SIDEBAR_CATEGORY_NAME)){
			sidebarCategory = rootCategory.getAllSubCategory(defaultSidebarCategoryId);
		} else{
			sidebarCategory = (Category) session.getAttribute(Constants.SIDEBAR_CATEGORY_NAME);
		}
		
		if(null == session.getAttribute(Constants.SHOPPING_CART_NAME)){
			shoppingCart = new ShoppingCart();
		} else{
			shoppingCart = (ShoppingCart) session.getAttribute(Constants.SHOPPING_CART_NAME);
		}
		
		session.setAttribute(Constants.ROOT_CATEGORY_NAME, rootCategory);
		session.setAttribute(Constants.SIDEBAR_CATEGORY_NAME, sidebarCategory);
		session.setAttribute(Constants.SHOPPING_CART_NAME, shoppingCart);
		
		filterChain.doFilter(request, response);
	}

	public void destroy() {
		//DO NOTHING
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		//DO NOTHING
	}

	/**
	 * @return the defaultSidebarCategoryId
	 */
	public Long getDefaultSidebarCategoryId() {
		return defaultSidebarCategoryId;
	}

	/**
	 * @param defaultSidebarCategoryId the defaultSidebarCategoryId to set
	 */
	public void setDefaultSidebarCategoryId(Long defaultSidebarCategoryId) {
		this.defaultSidebarCategoryId = defaultSidebarCategoryId;
	}

	/**
	 * @return the browseBooksService
	 */
	public BrowseBooksService getBrowseBooksService() {
		return browseBooksService;
	}

	/**
	 * @param browseBooksService the browseBooksService to set
	 */
	public void setBrowseBooksService(BrowseBooksService browseBooksService) {
		this.browseBooksService = browseBooksService;
	}

}
